/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.IngresoMaterial;
import entidades.LoteIngresoMaterial;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author g
 */
public class LoteIngresoMaterialJpaController implements Serializable {

    public LoteIngresoMaterialJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(LoteIngresoMaterial loteIngresoMaterial) {
        if (loteIngresoMaterial.getLs_ingresos() == null) {
            loteIngresoMaterial.setLs_ingresos(new ArrayList<IngresoMaterial>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<IngresoMaterial> attachedLs_ingresos = new ArrayList<IngresoMaterial>();
            for (IngresoMaterial ls_ingresosIngresoMaterialToAttach : loteIngresoMaterial.getLs_ingresos()) {
                ls_ingresosIngresoMaterialToAttach = em.getReference(ls_ingresosIngresoMaterialToAttach.getClass(), ls_ingresosIngresoMaterialToAttach.getId());
                attachedLs_ingresos.add(ls_ingresosIngresoMaterialToAttach);
            }
            loteIngresoMaterial.setLs_ingresos(attachedLs_ingresos);
            em.persist(loteIngresoMaterial);
            for (IngresoMaterial ls_ingresosIngresoMaterial : loteIngresoMaterial.getLs_ingresos()) {
                LoteIngresoMaterial oldLoteIngresoMaterialOfLs_ingresosIngresoMaterial = ls_ingresosIngresoMaterial.getLoteIngresoMaterial();
                ls_ingresosIngresoMaterial.setLoteIngresoMaterial(loteIngresoMaterial);
                ls_ingresosIngresoMaterial = em.merge(ls_ingresosIngresoMaterial);
                if (oldLoteIngresoMaterialOfLs_ingresosIngresoMaterial != null) {
                    oldLoteIngresoMaterialOfLs_ingresosIngresoMaterial.getLs_ingresos().remove(ls_ingresosIngresoMaterial);
                    oldLoteIngresoMaterialOfLs_ingresosIngresoMaterial = em.merge(oldLoteIngresoMaterialOfLs_ingresosIngresoMaterial);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(LoteIngresoMaterial loteIngresoMaterial) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            LoteIngresoMaterial persistentLoteIngresoMaterial = em.find(LoteIngresoMaterial.class, loteIngresoMaterial.getId());
            List<IngresoMaterial> ls_ingresosOld = persistentLoteIngresoMaterial.getLs_ingresos();
            List<IngresoMaterial> ls_ingresosNew = loteIngresoMaterial.getLs_ingresos();
            List<IngresoMaterial> attachedLs_ingresosNew = new ArrayList<IngresoMaterial>();
            for (IngresoMaterial ls_ingresosNewIngresoMaterialToAttach : ls_ingresosNew) {
                ls_ingresosNewIngresoMaterialToAttach = em.getReference(ls_ingresosNewIngresoMaterialToAttach.getClass(), ls_ingresosNewIngresoMaterialToAttach.getId());
                attachedLs_ingresosNew.add(ls_ingresosNewIngresoMaterialToAttach);
            }
            ls_ingresosNew = attachedLs_ingresosNew;
            loteIngresoMaterial.setLs_ingresos(ls_ingresosNew);
            loteIngresoMaterial = em.merge(loteIngresoMaterial);
            for (IngresoMaterial ls_ingresosOldIngresoMaterial : ls_ingresosOld) {
                if (!ls_ingresosNew.contains(ls_ingresosOldIngresoMaterial)) {
                    ls_ingresosOldIngresoMaterial.setLoteIngresoMaterial(null);
                    ls_ingresosOldIngresoMaterial = em.merge(ls_ingresosOldIngresoMaterial);
                }
            }
            for (IngresoMaterial ls_ingresosNewIngresoMaterial : ls_ingresosNew) {
                if (!ls_ingresosOld.contains(ls_ingresosNewIngresoMaterial)) {
                    LoteIngresoMaterial oldLoteIngresoMaterialOfLs_ingresosNewIngresoMaterial = ls_ingresosNewIngresoMaterial.getLoteIngresoMaterial();
                    ls_ingresosNewIngresoMaterial.setLoteIngresoMaterial(loteIngresoMaterial);
                    ls_ingresosNewIngresoMaterial = em.merge(ls_ingresosNewIngresoMaterial);
                    if (oldLoteIngresoMaterialOfLs_ingresosNewIngresoMaterial != null && !oldLoteIngresoMaterialOfLs_ingresosNewIngresoMaterial.equals(loteIngresoMaterial)) {
                        oldLoteIngresoMaterialOfLs_ingresosNewIngresoMaterial.getLs_ingresos().remove(ls_ingresosNewIngresoMaterial);
                        oldLoteIngresoMaterialOfLs_ingresosNewIngresoMaterial = em.merge(oldLoteIngresoMaterialOfLs_ingresosNewIngresoMaterial);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = loteIngresoMaterial.getId();
                if (findLoteIngresoMaterial(id) == null) {
                    throw new NonexistentEntityException("The loteIngresoMaterial with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            LoteIngresoMaterial loteIngresoMaterial;
            try {
                loteIngresoMaterial = em.getReference(LoteIngresoMaterial.class, id);
                loteIngresoMaterial.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The loteIngresoMaterial with id " + id + " no longer exists.", enfe);
            }
            List<IngresoMaterial> ls_ingresos = loteIngresoMaterial.getLs_ingresos();
            for (IngresoMaterial ls_ingresosIngresoMaterial : ls_ingresos) {
                ls_ingresosIngresoMaterial.setLoteIngresoMaterial(null);
                ls_ingresosIngresoMaterial = em.merge(ls_ingresosIngresoMaterial);
            }
            em.remove(loteIngresoMaterial);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<LoteIngresoMaterial> findLoteIngresoMaterialEntities() {
        return findLoteIngresoMaterialEntities(true, -1, -1);
    }

    public List<LoteIngresoMaterial> findLoteIngresoMaterialEntities(int maxResults, int firstResult) {
        return findLoteIngresoMaterialEntities(false, maxResults, firstResult);
    }

    private List<LoteIngresoMaterial> findLoteIngresoMaterialEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(LoteIngresoMaterial.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public LoteIngresoMaterial findLoteIngresoMaterial(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(LoteIngresoMaterial.class, id);
        } finally {
            em.close();
        }
    }

    public int getLoteIngresoMaterialCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<LoteIngresoMaterial> rt = cq.from(LoteIngresoMaterial.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
